<script src="assets/js/facewall-like.js"></script>
<script src="assets/js/mustache.js"></script>
<script src="assets/js/image-ready.js"></script>
<link href="assets/css/facewall.css" type="text/css" rel="stylesheet" />
<input type="hidden" id="load_count" value=<?php echo $photos[count($photos)-1]['photo_id'] ?> />

<script type="text/javascript">
        /*$(function(){
            $("img.facewall_single_pic").each(function(){
                var $this=$(this);
                imgReady($this.attr('src'),function(){
                    $this.css("height",this.height);
                    $this.css("width",this.width);
					//alert('size ready: width=' + this.width + '; height=' + this.height);
                    
                });
            });
        });*/
</script>
		
<?php
    function getImg($photo) {
        return '<img src="' . getURL($photo) . '" name="facwall_pic"  
					 alt="' . $photo['photo_title'] . '" class="facewall_single_pic"/>'; 
				
    } 
	
	function getURL($photo) {
		$str = $photo['location'];
		$type = substr($str,strlen($str)-4);
				
		if(($type != 'jpeg')&&($type != 'JPEG'))
			$str='upload/thumbs/'.substr($str,7,strlen($str)-11).'_thumb'.$type;
		else if  ($type == 'jpeg') 
			$str='upload/thumbs/'.substr($str,7,strlen($str)-12).'_thumb.jpeg';
		else $str='upload/thumbs/'.substr($str,7,strlen($str)-12).'_thumb.JPEG';
			
		if (file_exists($str))
			return $str;
		else 
			return $photo['location'];   //for old version only;
    }
	
	function getAnchor($photo) {
        return '<a href="post/show/' . $photo['post_id'] . '">' . getImg($photo) . '</a>';
    }
			
	function getLikenum($photo){
		return $photo['photo_like'];
	}
			
	function getLikeid($photo){
		return $photo['photo_id'];
	}
	
	function getposttime($photo){
	if (substr($photo['post_time'],0,10)=== date("Y-m-d"))
		return '今天';
	else if(substr($photo['post_time'],0,10)=== date("Y-m-d",time() - 86400))
		return '昨天';
	else if(substr($photo['post_time'],0,10)=== date("Y-m-d",time() - 172800))
		return '前天';
	else	return 'n天前';
	}
	
	function getposttitle($photo){
		
		if (strlen($photo['post_title'])<=30) return $photo['post_title'];
		else return mb_substr($photo['post_title'],0,12).'...';
		//return $photo['if_like'];//test facewall show!
	}
	
	function getallposttitle($photo){
		return $photo['post_title'];
	}
	
	function getposttopic($photo){
		/*
		if ($photo['post_topic']==0){
			return 'border_zibao';
		}
		else if ($photo['post_topic']==1){
			return 'border_guapai';
		}
		else if ($photo['post_topic']==2){
			return 'border_zhenyou';
		}
		
		else */return 'border_wrap';
	}
	
		function getLikeBtn($photo){
		if ($photo['if_like']==1) return 'liked_btn';
		else return 'like_btn';
	}
					
	function getImgRatio($photo){
		$ImgRatio = floor(($photo['height']/$photo['width'])*300);
		return $ImgRatio;
		
	}
			
    function getCol($photos, $colnum) {
		$c = -1;
		$heightofcolone = 0;
		$heightofcoltwo = 10;
		$heightofcolthree = 20;
		$heightofcolfour = 30;
			
		
        for ($i = 0; $i < count($photos); $i++) {
			
				switch (min($heightofcolone,$heightofcoltwo,$heightofcolthree,$heightofcolfour)){	
					case $heightofcolone: {$c=0;$heightofcolone += (getImgRatio($photos[$i])+80);break;}
					case $heightofcoltwo: {$c=1;$heightofcoltwo += (getImgRatio($photos[$i])+80);break;}
					case $heightofcolthree: {$c=2;$heightofcolthree += (getImgRatio($photos[$i])+80);break;}
					case $heightofcolfour: {$c=3;$heightofcolfour += (getImgRatio($photos[$i])+80);break;}
					default:alert("facewall.php line71");
			}		
				//echo $heightofcolone.";".$heightofcoltwo.";".$heightofcolthree;
		
					
			if ($c == $colnum) {
					showPhoto($photos[$i]);
            }
        }
			
	}
	
	function showPhoto($photo){
		
		echo ('<div class="' .getposttopic($photo). '" ><div class="border-inner">');
		echo getAnchor($photo);
		//echo '<span class="photo_time"> ' .getposttime($photo). ' </span>';		
			
		echo ('<button type="button" id="' .getLikeid($photo). '" class="' .getLikeBtn($photo).'" title="顶一下" >'
				 .getLikenum($photo). '</button>');	
		echo '<div class="post_topic" title="'.$photo['post_title'].'"> ' .getposttitle($photo). ' </div>';
		echo '</div></div>';
		echo "<br>";
		//print_r($photo);
	}
	
	/*
	function shuffle($photos){
	$max=count($photos);
	for($i=0;$i<$max;$i++)
	{
	$j=rand()%($max-$i)+$i;
	$temp = $photos[$i];
	$photos[$i]=$photos[$j];
	$photos[$j]=$temp;
	}
	return $photos;
	}*/
?>
		
		
<!-- number of columns -->
<?php shuffle($photos); ?>
<div class="container">
    <div class="span_facewall" style="margin-bottom:0px;" id="facewall_column0">
        <?php getCol($photos, 0); ?>			
    </div>
	
    <div class="span_facewall" id="facewall_column1">		
		<?php getCol($photos, 1); ?>			
    </div>
			
    <div class="span_facewall" id="facewall_column2">
        <?php getCol($photos, 2); ?>			
    </div>
	
	<div class="span_facewall" id="facewall_column3">
        <?php getCol($photos, 3); ?>			
    </div>
		
</div>

<div id="post_more">
    <div class="more_btn" >
        <a href="javascript:void(0);" id="morebtn">点击此处加载更多...</a>
    </div>
</div>